<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="服务器" prop="serviceName">
        <el-select v-model="queryParams.serviceId" placeholder="请选择">
          <el-option
            v-for="item in serviceLists"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="参数名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入steam64id或玩家名字"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-table stripe v-loading="loading" :data="configList">
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="steamID" align="center" prop="steamID" :show-overflow-tooltip="true">
        <template slot-scope="{ row }">
          <el-link :href="'http://yy.xzcc.cc/steam.php?id=' + row.onlineIds[1]" type="primary" target="_blank">
            {{ row.onlineIds[1] }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="玩家名称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="小队ID" align="center" prop="squadId" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="小队名称" align="center" prop="squadVO.squadName" :show-overflow-tooltip="true"/>-->
      <el-table-column label="团队ID" align="center" prop="teamId" :show-overflow-tooltip="true"/>
      <el-table-column label="角色" align="center" prop="role" :show-overflow-tooltip="true"/>
      <el-table-column label="是否领导" align="center" prop="leader" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="350">
        <template #default="scope">
          <el-button size="small" type="primary" @click="handleSide(scope.row.id)">跳边</el-button>
          <el-button size="small" type="warning" @click="handleWarn(scope.row.id)">警告</el-button>
          <el-button size="small" type="primary" plain @click="handleKick(scope.row.id)">踢出</el-button>
          <el-button size="small" type="danger" @click="handleBan(scope.row.id)">封禁</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog :visible.sync="visibleKick" title="提出玩家" width="500px">
      <el-form :model="kickData">
        <el-form-item label="请输入踢出原因:">
          <el-input v-model="kickData.reason"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <span class="dialog-footer">
            <el-button @click="visibleKick = false">取消</el-button>
            <el-button type="primary" @click="okKick"> 确定 </el-button>
        </span>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="visibleWarn" title="警告玩家" width="500px">
      <el-form :model="warnData">
        <el-form-item label="请输入警告内容:">
          <el-input v-model="warnData.reason"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <span class="dialog-footer">
            <el-button @click="visibleWarn = false">取消</el-button>
            <el-button type="primary" @click="okWarn"> 确定 </el-button>
        </span>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="visibleBan" title="封禁玩家" width="500px">
      <el-form :model="banData">
        <el-form-item label="封禁时长">
          <el-input v-model="banData.ban_time"/>
        </el-form-item>
        <el-form-item label="封禁原因">
          <el-input type="text" v-model="banData.ban_reason"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <span class="dialog-footer">
            <el-button @click="visibleBan = false">取消</el-button>
            <el-button type="primary" @click="okBan"> 确定 </el-button>
        </span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getGamerList } from '@/api/suqad/player'
import { kickPlayer, warnPlayer, banPlayer, changePlayerSide, disbandSquad } from '@/api/suqad/rcon'
import store from '@/store'

export default {
  name: 'Player',
  dicts: ['sys_yes_no'],
  data() {
    return {
      visibleWarn: false,
      warnData: {
        reason: null,
        steamid: null
      },
      visibleKick: false,
      kickData: {
        reason: null,
        steamid: null
      },
      visibleBan: false,
      banData: {
        ban_time: null,
        ban_reason: null,
        steamid: null
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      serviceLists: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        serviceId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        configName: [
          { required: true, message: '参数名称不能为空', trigger: 'blur' }
        ],
        configKey: [
          { required: true, message: '参数键名不能为空', trigger: 'blur' }
        ],
        configValue: [
          { required: true, message: '参数键值不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.serviceLists = store.getters.serviceLists
    this.queryParams.serviceId = this.serviceLists[0].id
    this.getList()
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true
      getGamerList(this.queryParams).then(response => {
          this.configList = response.rows
          this.total = response.total
          this.loading = false
        }
      ).catch(e => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        configId: undefined,
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: 'Y',
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    }
    /**
     * 跳变
     * @param row
     */
    , handleSide(row) {
      this.$confirm('确定要跳边吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        changePlayerSide({
          serviceId: this.queryParams.serviceId,
          id: row
        }).then(res => {
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
    /**
     * 警告
     * @param row
     */
    , handleWarn(row) {
      this.warnData.steamid = row
      this.visibleWarn = true
    },
    okWarn() {
      warnPlayer({
        serviceId: this.queryParams.serviceId,
        id: this.warnData.steamid,
        reason: this.warnData.reason
      }).then(res => {
        this.visibleWarn = false

      })
    }
    /**
     * 踢出
     * @param row
     */
    , handleKick(row) {
      this.kickData.steamid = row
      this.visibleKick = true
    },
    okKick() {
      kickPlayer({
        serviceId: this.queryParams.serviceId,
        id: this.kickData.steamid,
        reason: this.kickData.reason
      }).then(res => {
        this.visibleKick = false
      })
    }
    /**
     * 封禁
     * @param row
     */
    , handleBan(row) {
      this.banData.steamid = row
      this.visibleBan = true
    },
    okBan() {
      this.visibleBan = false
      banPlayer({
        serviceId: this.queryParams.serviceId,
        id: this.banData.steamid,
        time: this.banData.ban_time,
        reason: this.banData.reason
      }).then(res => {
        this.visibleBan = false
      })
    }

  }
}
</script>
